/*********************************************************
  Action Semantics like implementation

  Xavier Pi - 2009

 
Working APIs: 

http://argouml-stats.tigris.org/nonav/javadocs/javadocs-0.28/  (incomplet?)
http://argouml-stats.tigris.org/nonav/javadocs/javadocs-0.22/   (complet?)
http://www.andromda.org/docs-3.3/jmi-uml1.4/index.html
http://www.mozilla.org/rhino/ScriptingJava.html  (Rhino)
***********************************************************/

importPackage(org.argouml.model);
importPackage(org.argouml.kernel);
importPackage(org.argouml.uml.diagram.static_structure.ui);
importPackage(org.omg.uml.foundation.datatypes);
 
_pm = ProjectManager.getManager();
_p = _pm.getCurrentProject();
_m = _p.getModel();
_m.setName("xUMLModel");	
_cf = Model.getCoreFactory();
_pc = _p.getProfileConfiguration();
_dtf = Model.getDataTypesFactory();
_ch = Model.getCoreHelper();
_clf = Model.getCollaborationsFactory();
// _col = _clf.buildCollaboration(_m);   // _col is created and bound in XUmlDialog
_coh = Model.getCollaborationsHelper();
_f = Model.getFacade();


//  Action semantics for models

CreateObject = function(c, oid){
	cla = _pc.findTypeInModel(c, _m);
	o = _clf.buildClassifierRole(_col);
	if (oid)
		onam = oid;
	else
		onam = c.charAt(0).toLowerCase() + new java.util.Date().getTime();

	o.setName(onam);
	_coh.addBase(o, cla);
	return o	
}

DestroyObject = function(o){
	XUmlDialog.removeElementFromModel(o);
}

CreateLink = function(as, o1, o2){
	assoc = _cf.buildAssociation(o1, o2);
	assoc.setName(as);
	return assoc;
}

DestroyLink = function(as){
	XUmlDialog.removeElementFromModel(as);
}

AddStructuralFeature = function(o, at, v){

	cla = _f.getBases(o).toArray()[0];
	atrs = _f.getAttributes(cla).toArray();
	found = false;

	n = atrs.length;
	i = 0;
	while (i < n && !found){
		if (atrs[i].getName() == at){
			found = true;
			atrfound = atrs[i];
		}
		i++;
	}
	
	if (found){
		ex = _dtf.createExpression("", v);
		atrfound.setInitialValue(ex);
	}
}


//  Action semantics for the metamodel M2(UML)
// Could be defined with Action semantics for models with an specific metamodel

createClass = function(cla){
  return _cf.buildClass(cla, _m);
}

removeClass = function(cla){
	c = _pc.findTypeInModel(cla, _m);
	XUmlDialog.removeElementFromModel(c);
}

createAttribute = function(cla, attr, type, initialValue){
	ty = _pc.findType(type);
	
	if (ty != null){
		c = _pc.findTypeInModel(cla, _m);
		a = _cf.buildAttribute2(c, ty);
	}else{
		ty = _pc.findTypeInModel(type, _m);
		c = _pc.findTypeInModel(cla, _m);
		a = _cf.buildAttribute2(c, ty);
	}
	
	a.setName(attr);
	ex = _dtf.createExpression("", initialValue);
	a.setInitialValue(ex);
}

createAssociation = function(nameAssoc, multi1, cla1, multi2, cla2){
    c1 = _pc.findTypeInModel(cla1, _m);
    c2 = _pc.findTypeInModel(cla2, _m);
	as = _cf.buildAssociation(c1, c2);
	as.setName(nameAssoc);
	ae1 = _ch.getAssociationEnd(c1, as);
	ae2 = _ch.getAssociationEnd(c2, as);
	mul1 = _dtf.createMultiplicity(multi1);
	mul2 = _dtf.createMultiplicity(multi2);
	_ch.setMultiplicity(ae1, mul1);
	_ch.setMultiplicity(ae2, mul2);
	return as;
}

removeAssociation = function(as){
	XUmlDialog.removeElementFromModel(as);
}

echo = function(s){
	_tta2 = "" + _ta2.getText();
	if (_tta2 == ""){
		_ta2.setText(s);
	}else{
		_ta2.setText(_tta2 + "\n" + s);
	}
}

